﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class ProductClassDao
    {


        public ProductClassDao()
        {

            Mapper.CreateMap<E_ProductClass, ProductClassModel>();
        }
        public List<E_ProductClass> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_ProductClass.Count();

                return edm.E_ProductClass.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ProductClassModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ProductClass.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_ProductClass, ProductClassModel>(query);
            }

        }


        public int Save(ProductClassModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ProductClass.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_ProductClass();

                    edm.AddToE_ProductClass(query);

                }


                query.ClassName = obj.ClassName;

                query.OrderID = obj.OrderID;

                query.PreID = obj.PreID;
                query.SiteID = obj.SiteID;

                query.CreateUserId = obj.CreateUserId;
                edm.SaveChanges();

                return query.Id;


            }

        }
        public List<ProductClassModel> GetListByPid(int siteid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities()) {
                var query = edm.E_ProductClass.Where(s=>s.SiteID==siteid);
                return Mapper.Map<List<E_ProductClass>, List<ProductClassModel>>(query.ToList());

            }

        }



        public List<ProductClassModel> GetListByPid(int pid, int siteid)
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_ProductClass>, List<ProductClassModel>>(edm.E_ProductClass.Where(s=>s.PreID==pid&&s.SiteID==siteid).ToList());


              //  return dao.GetListByPid(pid, siteid);
            }
        }
        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ProductClass.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

